package com.xh.rufongwallpaper.common.interceptor;

import com.xh.rufongwallpaper.common.constants.DeviceConstants;
import com.xh.rufongwallpaper.service.DeviceService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 设备认证拦截器
 *
 * @author XH
 */
@Slf4j
@Component
@RequiredArgsConstructor
public class DeviceAuthInterceptor implements HandlerInterceptor {

    private final DeviceService deviceService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        // 获取设备ID
        String deviceId = request.getHeader(DeviceConstants.DEVICE_ID_HEADER);

        if (StringUtils.isNotBlank(deviceId)) {
            // 更新设备最后活跃时间
            deviceService.updateDeviceActiveTime(deviceId);
        }
        // 放行请求
        return true;
    }
} 